package com.test.converter;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.test.bean.Point;

public class PointConverter3 extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
		
		List<Point> list = new ArrayList<Point>();
		
		for( String arg : arg1){
			
			Point point = new Point();
			
			String[] paramValues = arg.split(",");
			
			int x = Integer.parseInt(paramValues[0]);
			int y = Integer.parseInt(paramValues[1]);
			
			point.setX(x);
			point.setY(y);
			
			list.add(point);
			
		}
		return list;
	}

	@Override
	public String convertToString(Map arg0, Object arg1) {
		
		List<Point> list = (List<Point>)arg1;
		
		StringBuilder sb = new StringBuilder();
		sb.append("[ ");
		int number = 0;
		for(Point point : list){
			
			++number;
			int x = point.getX();
			int y = point .getY();
			
			sb.append(number).append("x = ").append(x).append(" , y = ").append(y).append("    ");
			
		}
		sb.append("]");
		return sb.toString();
	}
		
}
